$(function () {
    //获取url
    var url = localStorage.getItem("ajaxUrl");
    var pass = 1;

    //点击返回箭头 清除 缓存 打开 storekeeper.html
    $(".head .goBack").on("click", function () {
        localStorage.removeItem("work");
        window.location.href = "acidTransferFH.html";
    });
    //点击 取消
    $(".foot .del_receive").on("click", function () {
        localStorage.removeItem("work");
        window.location.href = "acidTransferFH.html";
    });

    var work = JSON.parse(localStorage.getItem("work"));

    $(".content .ydh").text(work.QRDara.bill_no);
    $(".content .cph").text(work.QRDara.plate_no);
    $(".content .sj").text(work.QRDara.driver_name);
    $(".content input").val("");
    $(".content input.count").focus();

    /*//批号   获取 焦点
    $("input#goods_id").focus();

    //根据扫描内容 得到 信息
    $("#goods_id").on("keydown", function (event) {
        if (event.keyCode == "13") {
            if (pass == 0) {
                return
            }
            pass = 0;
            var val = $(this).val();
            //$("#goods_id").blur();
            if (!val) {
                $alert("请扫描司机二维码！");
                pass = 1;
                setTimeout(function () {
                    $("#goods_id").val("").focus();
                }, 1400);
                return;
            }
            //加载提示符 显示
            loading_show("搜索中...");
            //获取工单
            $.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "post",
                url: url + '/app/acidTransfer/getCarQR',
                data: {
                    _id: work.id,
                    QR: val
                },
                dataType: 'json',
                json: 'callback',
                success: function (res) {
                    pass = 1;
                    //加载提示符 隐藏
                    loading_hide();
                    if (res.success) {
                        var data = res.result;
                        $("#goods_id").val("");
                        console.log(data);
                        $alert("扫描成功", 800);
                        $(".content .ydh").text(data.bill_no);
                        $(".content .cph").text(data.plate_no);
                        $(".content .sj").text(data.driver_name);
                        $(".content input").val("");
                        $(".content input.count").focus();
                    } else {
                        setTimeout(function () {
                            $("#goods_id").val("").focus();
                        }, 1400);
                        $alert(res.message);
                    }
                },
                error: function (error) {
                    pass = 1;
                    //加载提示符 隐藏
                    loading_hide();
                    console.log(error);
                    setTimeout(function () {
                        $("#goods_id").val("").focus();
                    }, 1400);
                    $alert(error.responseText);
                }
            });
        }
    });*/

    //点击 确认
    $(".foot").on("click", ".add_receive", function () {
        if (pass == 0) {
            return
        }
        pass = 0;
        var ydh = $(".content .ydh").text();
        //判断数据
        if (!ydh || ydh == "空") {
            $alert("请扫描司机二维码！");
            pass = 1;
            return;
        } else if (!$("input.count").val()) {
            $alert("请输入件数！");
            pass = 1;
            return;
        } else if (Number($("input.count").val()) < 10 || Number($("input.count").val()) > 20) {
            $alert("件数应是10-20之间！");
            pass = 1;
            return;
        }
        //获取 信息
        loading_show("提交中...");
        $.ajax({
            headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
            type: "post",
            url: url + '/app/acidTransfer/saveDriverQrcode',
            data: {
                _id: work.id,
                bill_no: $(".content .ydh").text(),
                plate_no: $(".content .cph").text(),
                driver_name: $(".content .sj").text(),
                count: $("input.count").val(),
            },
            dataType: "json",
            json: "callback",
            timeout: 60000,
            cache: false,
            success: function (res) {
                pass = 1;
                loading_hide();
                if (res.success) {
                    console.log("酸洗调拨发货成功");
                    $alert("酸洗调拨发货成功！");
                    setTimeout(function () {
                        localStorage.removeItem("work");
                        window.location.href = "acidTransferFH.html";
                    }, 800);
                } else {
                    console.log(res);
                    $alert(res.message);
                }
            },
            error: function (error) {
                pass = 1;
                if (error.status == 0) {
                    loading_hide();
                    $alert("请求超时，请重新提交！");
                } else {
                    loading_hide();
                    console.log(error);
                    $alert(error.responseText);
                }
            }
        });
    });

});

